package com.blb.dao.admin.impl;

import com.blb.dao.admin.AdminDao;
import com.blb.pojo.Admin;
import com.blb.utils.JDBCUtils;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Repository
public class AdminDaoImpl implements AdminDao {

    @Override
    public Admin selectByName(String name) throws Exception {
        Connection connection = JDBCUtils.getConnection();
        Admin admin = JDBCUtils.getOne(connection, Admin.class, "select * from t_admin where name = ?", name);
        connection.close();
        return admin;
    }

//
    @Override
    public List<Admin> selectPageByName(int page, int size, String name) throws Exception {
        Connection connection = JDBCUtils.getConnection();
        String sql = "select * from t_admin ";
        ArrayList<Object> params = new ArrayList<>();

        if(name!=null && !name.isEmpty()){
            sql+=" where name like ? ";
            params.add("%"+name+"%");
        }
        sql+=" limit ?,?";
        params.add((page-1)*size);
        params.add(size);

String a =           "select * from t_adminwhere name like ?"                ;

        List<Admin> list = JDBCUtils.getList(connection, Admin.class, sql, params.toArray());
        connection.close();
        return list;
    }

    @Override
    public int count(String name) throws SQLException, ClassNotFoundException {
        Connection connection = JDBCUtils.getConnection();
        String sql = "select count(id) count from t_admin ";
        ArrayList<Object> params = new ArrayList<>();

        if(name!=null && !name.isEmpty()){
            sql+=" where name like ?";
            params.add("%"+name+"%");
        }

        ResultSet resultSet = JDBCUtils.prepareStatement(connection, sql, params.toArray()).executeQuery();
        resultSet.next();
        int count = resultSet.getInt("count");
        connection.close();
        return count;
    }

    @Override
    public void insert(Admin admin) throws SQLException, ClassNotFoundException {
        Connection connection = JDBCUtils.getConnection();
        JDBCUtils.prepareStatement(connection, "insert into t_admin(name, pwd, img) values(?,?,?)",
                admin.getName(), admin.getPwd(), admin.getImg()).executeUpdate();
        connection.close();
    }

    @Override
    public void deleteById(int id) throws SQLException, ClassNotFoundException {
        Connection connection = JDBCUtils.getConnection();
        JDBCUtils.prepareStatement(connection, "delete from t_admin where id = ?", id).executeUpdate();
        connection.close();
    }

    @Override
    public void batchDelete(String[] ids) throws SQLException, ClassNotFoundException {
        String sql = "delete from t_admin where id in (";

        for (int i = 0; i < ids.length; i++) {
            sql+="?";
            if (i != ids.length-1){
                sql+=",";
            }
        }
        sql+=")";

        Connection connection = JDBCUtils.getConnection();
        JDBCUtils.prepareStatement(connection,sql,ids).executeUpdate();
        connection.close();
    }

    @Override
    public Admin selectById(int id) throws Exception {
        Connection connection = JDBCUtils.getConnection();
        Admin admin = JDBCUtils.getOne(connection, Admin.class, "select * from t_admin where id = ?", id);
        connection.close();
        return admin;
    }

    @Override
    public void update(Admin admin) throws SQLException, ClassNotFoundException {
        Connection connection = JDBCUtils.getConnection();
        JDBCUtils.prepareStatement(connection,"update t_admin set name=?,pwd=?,img=? where id=?"
                ,admin.getName(),admin.getPwd(),admin.getImg(),admin.getId()).executeUpdate();
        connection.close();
    }
}
